package windbell.domain.risk.impl;

import com.google.common.base.Objects;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;
import windbell.domain.risk.RiskCategory;
import windbell.domain.risk.RiskSubCategory;
import windbell.domain.risk.impl.BaseHasSimplePyImpl;
import windbell.domain.risk.impl.RiskSubCategoryImpl;
import windbell.domain.risk.proxy.RiskCategoryProxy;

@Entity
@DiscriminatorValue("RISKCATEGORY_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "riskcategory")
public class RiskCategoryImpl extends BaseHasSimplePyImpl implements RiskCategory {
  public RiskCategoryImpl() {
    super();
  }
  
  public RiskCategoryImpl(final String code) {
    super();
    this.code = code;
  }
  
  private String code;
  
  public String getCode() {
    return this.code;
  }
  
  public RiskCategory setCode(final String code) {
    this.code = code;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RiskSubCategoryImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "category")
  private Collection<RiskSubCategory> subCategory = new java.util.ArrayList<windbell.domain.risk.RiskSubCategory>();;
  
  public Collection<RiskSubCategory> getSubCategory() {
    return this.subCategory;
  }
  
  public RiskSubCategory createAndAddtoSubCategory() {
    getSubCategory().size();
    windbell.domain.risk.RiskSubCategory riskSubCategory = new windbell.domain.risk.impl.RiskSubCategoryImpl(this);
    getObjectFactory().create(riskSubCategory);
    getSubCategory().add(riskSubCategory);
    return riskSubCategory;
    
  }
  
  public RiskSubCategory createAndAddtoSubCategory(final Procedure1<RiskSubCategory> updater) {
    getSubCategory().size();
    windbell.domain.risk.RiskSubCategory riskSubCategory = new windbell.domain.risk.impl.RiskSubCategoryImpl(this);
    getObjectFactory().create(riskSubCategory);
    getSubCategory().add(riskSubCategory);
    return riskSubCategory;
    
  }
  
  public RiskCategory removeAllSubCategory() {
    for(RiskSubCategory obj : this.subCategory){
    	getObjectFactory().delete(obj);
    }
    this.subCategory.clear();
    return this;	
    
  }
  
  public RiskCategory removeFromSubCategory(final RiskSubCategory riskSubCategory) {
    this.subCategory.remove(riskSubCategory);
    getObjectFactory().delete(riskSubCategory);
    return this;
    
  }
  
  public void checkValid() {
    boolean _notEquals = (!Objects.equal(this.subCategory, null));
    Validates.Assert(_notEquals, "\u5C0F\u7C7B\u4E0D\u80FD\u4E3A\u7A7A");
  }
  
  public static RiskCategory create(final String code, final IObjectFactory objectFactory) {
    windbell.domain.risk.RiskCategory riskCategory = new windbell.domain.risk.impl.RiskCategoryImpl(
    	code
    );
    objectFactory.create(riskCategory);
    return riskCategory;			
    
  }
  
  public static RiskCategory createRiskCategoryImpl(final String code, final IObjectFactory objectFactory, final Procedure1<RiskCategory> init) {
    windbell.domain.risk.RiskCategory riskCategory = new windbell.domain.risk.impl.RiskCategoryImpl(
    	code
    );
    objectFactory.create(riskCategory);
    init.apply(riskCategory);
    return riskCategory;			
    
  }
  
  public RiskCategoryProxy toProxy() {
    windbell.domain.risk.proxy.RiskCategoryProxy proxy = new windbell.domain.risk.proxy.RiskCategoryProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
